package com.gs.spider.tq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.CharEncoding;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by TQ on 2017/5/25.
 */
public class ERPCollect {

    private String token = "";

    private CloseableHttpClient loginErp(CloseableHttpClient client) {
//		CloseableHttpClient client  = HttpClients.custom().build();
        String str = HttpUtil.doGet("http://120.40.103.185:8899/", CharEncoding.UTF_8, client);
        Matcher m = Pattern.compile("document.getElementById.('pass'.).value=hex_hmac_md5.(\"" + "(.*)" + "\",pwd.);").matcher(str);
        String key = null;
        if (m.find())
            key = m.group(3);
        //String pass = Encryption.hamcMd5(key, "06a79260bced87fedc221ae20ffc4632");
        Map<String, String> params = new HashMap<String, String>();
        params.put("validate", "login");
        params.put("userId", "5150262");
        params.put("pass", "xxfxxf");
        params.put("image.x", "29");
        params.put("image.y", "6");
        String result = HttpUtil.doPost("http://120.40.103.185:8899/login.do", CharEncoding.UTF_8, params, client);
        this.token = key;

        return client;
    }

    private JSONObject collectJusticeDeal(CloseableHttpClient client, String start, String limit, String token) {
        String reqUrl = "http://120.40.103.185:8899/UIProcessor?Table=Entity_SCGJ&Token={token}".replace("{token}", token);
        Map<String, String> params = new HashMap<String, String>();
        params.put("start", start);
        params.put("limit", limit);
        params.put("loadStore", "true");
        String listResult = HttpUtil.doPost(reqUrl, CharEncoding.UTF_8, params, client);
        listResult = listResult.replaceAll("\"[^\":,]*\":[^\\(:,]*\\([^\\)]*\\),", "");
        JSONObject obj = JSON.parseObject(listResult);
        return obj;
    }

    public static void main(String[] args) {
        ERPCollect service = new ERPCollect();
        CloseableHttpClient client = HttpClients.custom().build();
        client = service.loginErp(client);
        //http://120.40.103.185:8899/UIProcessor?Table=Entity_SCGJ&Token
        String result = HttpUtil.doGet("http://120.40.103.185:8899/UIProcessor?operate=View&Table=Entity_SCGJ&Token" + service.token, CharEncoding.UTF_8, client);
        Matcher m = Pattern.compile("token : '" + "(.*)" + "',").matcher(result);
        String token = null;
        if (m.find())
            token = m.group(1);
        JSONObject obj = service.collectJusticeDeal(client, "0", "50", token);
        String results = obj.getString("result");
        JSONObject resultObj = service.collectJusticeDeal(client, "0", results, token);
        Integer size = resultObj.getJSONArray("records").size();
        System.out.println(size);
        resultObj.getJSONArray("records");
    }

}
